<template>
  <a-card style="margin-top:-2px" :bordered="false">
    <div class="">
      <a-form>
        <!--查询条件 start-->
        <a-row class="form-row" >
          <a-col :span="6" :offset="1">
            <a-form-item
              label="考评名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input v-model="queryParam.kpmc" placeholder="请输入考评名称" allowClear/>
            </a-form-item>
          </a-col>
          <a-col :span="6" :offset="1">
            <a-form-item
              label="单位"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-select v-model="queryParam.dw" placeholder="请选择单位">
                <a-select-option v-for="d in orgList" :value="d.ID" :key="d.ID">
                  {{ d.ORGNAME }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6" :offset="1">
            <a-form-item
              label="考评人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input v-model="queryParam.kpr" placeholder="请输入考评人" allowClear/>
            </a-form-item>
          </a-col>
          <a-col :span="2" :offset="1">

          </a-col>
        </a-row>
        <a-row class="form-row" >
          <a-col :span="6" :offset="1">
            <a-form-item
              label="考评开始日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-range-picker
                format="YYYY-MM-DD"
                valueFormat="YYYY-MM-DD"
                v-model="queryParam.kpkssjArr"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6" :offset="1">
            <a-form-item
              label="考评结束日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-range-picker
                format="YYYY-MM-DD"
                valueFormat="YYYY-MM-DD"
                v-model="queryParam.kpjssjArr"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8" :offset="1">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="search">查询</a-button>
              <a-button style="margin-left: 8px" @click="getRole">重置</a-button>
            </span>
            <span style="float:right" class="table-page-search-submitButtons">
              <a-button type="primary" style="margin-left: 8px;float:right" @click="openAdd" >新增</a-button>
            </span>
          </a-col>
        </a-row>
        <!--查询条件 end-->
      </a-form>
    </div>
    <s-table
      ref="stable"
      size="default"
      :columns="columns"
      :data="loadData"
      style="margin-top:10px"
    >
      <span slot="action" slot-scope="text, record">
        <a @click="openDeduction(record)" v-show="(queryParam.roleType === '2') || (queryParam.roleType === '1' && record.kpzt === '1') ">记成效记差错</a>
        <a-divider type="vertical" v-show="(queryParam.roleType === '2') || (queryParam.roleType === '1' && record.kpzt === '1') "/>
        <a @click="delScore(record)" v-show="(queryParam.roleType === '2') || (queryParam.roleType === '1' && record.kpzt === '1') ">删除</a>
        <a-divider type="vertical" v-show="((queryParam.roleType === '2') || (queryParam.roleType === '1' && record.kpzt === '1') )&&record.kpzt === '1'"/>
        <a @click="sbScore(record)" v-show="record.kpzt === '1'">上报</a>
      </span>
    </s-table>
    <!-- 弹出框 -->
    <person-score-manage-add ref="personScoreManageAdd" @close="search"></person-score-manage-add>
    <person-score-manage-record ref="personScoreManageRecord" @close="search"></person-score-manage-record>
  </a-card>
</template>
<script >
import { STable } from '@/components'
import { query, getOrgTree } from '@/api/admin'
import { personPage, getPlan, getAllOrg, updateScore } from '@/api/parBasicData'
import PersonScoreManageAdd from '@/views/par/personScoreManage/PersonScoreManageAdd'
import PersonScoreManageRecord from '@/views/par/personScoreManage/PersonScoreManageRecord'
import { TreeSelect } from 'ant-design-vue'
import { mapGetters } from 'vuex'
const SHOW_PARENT = TreeSelect.SHOW_PARENT
export default {
  components: {
    STable,
    PersonScoreManageAdd,
    PersonScoreManageRecord
  },
  data () {
    return {
      form: this.$form.createForm(this),
      // 查询参数
      queryParam: {
        roleType: '1' // 1是考评员，2是考评组长
      }, // 查询条件
      actionParam: {}, // 请求参数
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return personPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.result
          })
      },
      // 表头
      columns: [
        { title: '考评名称', dataIndex: 'kpmc', ellipsis: true },
        { title: '考评计划名称', dataIndex: 'kpjhmc', ellipsis: true },
        { title: '考评开始日期', dataIndex: 'kssj' },
        { title: '考评结束日期', dataIndex: 'jssj' },
        { title: '单位', dataIndex: 'dwlx' },
        { title: '考评人', dataIndex: 'kpr', ellipsis: true },
        { title: '检查方式',
          dataIndex: 'jcfs',
          customRender: function (attr1) {
            if (attr1 === '1') {
              return '四不两直检查'
            } else {
              return '定期检查'
            }
          },
          ellipsis: true
        },
        { title: '创建人', dataIndex: 'crtName' },
        { title: '创建时间', dataIndex: 'crtTime', ellipsis: true },
        { title: '考评状态',
          dataIndex: 'kpzt',
          customRender: function (attr1) {
            if (attr1 === '1') {
              return '保存'
            } else {
              return '上报'
            }
          } },
        { title: '操作', width: '220px', dataIndex: 'action', scopedSlots: { customRender: 'action' }
        }
      ],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      modelVisible: false, // 弹出框显示
      selectInfo: {}, // 选中的数据
      confirmLoading: false, // Loading
      orgTree: [],
      SHOW_PARENT,
      planList: [],
      orgList: []

    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  created () {
    this.init()
  },
  methods: {
    /**
     * 搜索
     */
    search () {
      if (this.queryParam.kpkssjArr) {
        this.queryParam.kssjStart = this.queryParam.kpkssjArr[0]
        this.queryParam.kssjEnd = this.queryParam.kpkssjArr[1]
      }
      if (this.queryParam.kpjssjArr) {
        this.queryParam.jssjStart = this.queryParam.kpjssjArr[0]
        this.queryParam.jssjEnd = this.queryParam.kpjssjArr[1]
      }
      this.$refs.stable.loadData({}, this.queryParam, {})
    },
    /**
     * 打开新增弹出框
     */
    openAdd () {
      this.$refs.personScoreManageAdd.add(this.orgTree)
    },
    /**
     * 打开修改弹出框
     */
    openEdit (record) {
      this.$refs.personScoreManageAdd.edit(record, this.orgTree)
    },
    /**
     * 打开记成效弹出框
     */
    openBonus (record) {
      this.$refs.personScoreManageRecord.bonus(record)
    },
    /**
     * 打开记差错弹出框
     */
    openDeduction (record) {
      this.$refs.personScoreManageRecord.deduction(record)
    },
    /**
     * 获取树形组织信息
     * */
    init () {
      getOrgTree({ 'parentTreeId': 'root' }).then(res => {
        this.orgTree = this.handleVal(res.result.data)
      })
      const param = {}
      param.scbj = '0'
      getAllOrg().then(res => {
        this.orgList = res
      })
      this.getRole()
    },
    getRole () {
      this.queryParam = {}
      const roleList = this.userInfo.userRole
      roleList.some(item => {
        if (item.name === '考评组长') {
          this.queryParam.roleType = '2'
          return true
        } else {
          this.queryParam.roleType = '1'
          return false
        }
      })
    },
    /**
     * 处理后台返回值 替换名字 id=>key label=>title
     */
    handleVal (value) {
      let str = JSON.stringify(value)
      str = str.replace(/id/g, 'value').replace(/label/g, 'title')
      return JSON.parse(str)
    },
    delScore (record) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: `确认要删除该条记录吗?`,
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          const param = {}
          param.id = record.id
          param.scbj = '1'
          updateScore(
            param
          ).then(
            res => {
              if (res.status === 200) {
                _this.$notification['success']({
                  message: '删除成功',
                  duration: 2
                })
                _this.visable = false
              } else {
                _this.$notification['error']({
                  message: res.message,
                  duration: 4
                })
              }
            }
          ).catch(() =>
            _this.$notification['error']({
              message: '出现异常，请联系系统管理员',
              duration: 4
            })
          ).finally(() => {
            _this.confirmLoading = false
            _this.search()
          })
        },
        onCancel: () => {
          this.$notification['info']({
            message: '取消删除操作',
            duration: 4
          })
        }
      })
    },
    sbScore (record) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: `确认要上报该条记录吗?`,
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          const param = {}
          param.id = record.id
          param.kpzt = '2'
          updateScore(
            param
          ).then(
            res => {
              if (res.status === 200) {
                _this.$notification['success']({
                  message: '上报成功',
                  duration: 2
                })
                _this.visable = false
              } else {
                _this.$notification['error']({
                  message: res.message,
                  duration: 4
                })
              }
            }
          ).catch(() =>
            _this.$notification['error']({
              message: '出现异常，请联系系统管理员',
              duration: 4
            })
          ).finally(() => {
            _this.confirmLoading = false
            _this.search()
          })
        },
        onCancel: () => {
          _this.$notification['info']({
            message: '取消上报操作',
            duration: 4
          })
        }
      })
    }

  }
}
</script>
<style scoped>

</style>
